import Vue from 'vue'
import Router from 'vue-router'
import login from './components/Login.vue'
import home from './components/Home.vue'
import users from './components/user/Users.vue'
import welcome from './components/Welcome.vue'
import roles from './components/power/Roles.vue'
import rights from './components/power/Rights.vue'
import categories from './components/goods/Cate.vue'
import params from './components/goods/Params.vue'
import goods from './components/goods/Goods.vue'
import orders from './components/order/Order.vue'
import reports from './components/report/Report.vue'
import add from './components/goods/Add.vue'
Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [
    { path: '/', redirect: '/login' },
    {
      path: '/login',
      component: login
    },
    {
      path: '/home',
      component: home,
      redirect: '/welcome',
      children: [
        {
          path: '/welcome',
          component: welcome
        },
        {
          path: '/users',
          component: users
        },
        {
          path: '/roles',
          component: roles
        },
        {
          path: '/rights',
          component: rights
        },
        {
          path: '/categories',
          component: categories
        },
        {
          path: '/params',
          component: params
        },
        {
          path: '/goods',
          component: goods
        },
        {
          path: '/orders',
          component: orders
        },
        {
          path: '/reports',
          component: reports
        },
        {
          path: '/goods/add',
          component: add
        }
      ]
    }
  ]
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to将要访问路径
  // from来的路径
  // next放行函数
  if (to.path === '/login') return next()
  // 获取token以校验身份
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) { return next('/login') }
  next()
})
export default router
